﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SalaDeEnsayo.ApplicationService;
using SalaDeEnsayo.ApplicationService.Model.Entity;

namespace SalaDeEnsayo.WebApplication
{
    public partial class Web : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //HabilitarLinksValidos();
            }
        }

        private void HabilitarLinksValidos()
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                UsuarioService usuarioS = new UsuarioService();
                //lnkLogout.Visible = true;
                Usuario usuario = usuarioS.GetUsuarioById(((SalaDeEnsayoPrincipal)HttpContext.Current.User).UsuarioId);
                litUserName.Text = String.Format("Bienvenido {0}", usuario.NombreCompleto);
            }
        }

        #region Métodos Publicos
        public void ShowException(Exception ex)
        {
            //if ((ex is SumeriaException && ((SumeriaException)ex).IsCritical)
            //    || (ex.InnerException != null && ex.InnerException is SumeriaException && ((SumeriaException)ex.InnerException).IsCritical))
            //{
            //    ContentPlaceHolder.Visible = false;
            //}

            // Si ya tengo un error, no muestro los siguientes.
            if (this.Message != null && !this.Message.Visible || this.Message.Attributes["class"] != "ErrorMessage")
            {
                this.Message.Visible = true;
                //if (DebugExceptionMessageHandling)
                //    this.Message.InnerHtml = FormatMessage(ex.ToString());
                //else
                this.Message.InnerHtml = FormatMessage(ex.Message);
                this.Message.Attributes["class"] = "ErrorMessage";
            }
        }

        public void ShowError(string message, params object[] parameters)
        {
            if (this.Message != null)
            {
                this.Message.Visible = true;
                this.Message.InnerHtml = FormatMessage(string.Format(message, parameters));
                this.Message.Attributes["class"] = this.ErrorClass();
            }
        }

        public virtual string ErrorClass()
        {
            return "ErrorMessage";
        }

        public void ShowAlert(string message, params object[] parameters)
        {
            if (this.Message != null)
            {
                this.Message.Visible = true;
                this.Message.InnerHtml = FormatMessage(string.Format(message, parameters));
                this.Message.Attributes["class"] = "AlertMessage";
            }
        }

        public void ShowMessage(string message, params object[] parameters)
        {
            if (this.Message != null)
            {
                this.Message.Visible = true;
                this.Message.InnerHtml = FormatMessage(string.Format(message, parameters));
                this.Message.Attributes["class"] = "NormalMessage";
            }
        }

        public void AddTrigger(Control control)
        {
            //var trigger = new PostBackTrigger();
            //trigger.ControlID= control.UniqueID;
            //this.AjaxPanel.Triggers.Add(trigger);
            ToolkitScriptManager1.RegisterPostBackControl(control);
        }

        #endregion

        #region Métodos privados

        private string FormatMessage(string message)
        {
            return message.Replace(Environment.NewLine, "<br/>");
        }
        #endregion
    }
}
